package com.xsl.culture.mybasevideoview.view;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xsl.culture.R;
import com.xsl.culture.mybasevideoview.utils.StringUtil;

import java.util.ArrayList;

public class ChapterAdapter extends RecyclerView.Adapter<ChapterAdapter.ChapterVideoHolder> {
    private int clickPos;
    private ArrayList<String> mDatas = null;

    ChapterAdapter(int curChapter) {
        clickPos = curChapter;
    }

    @NonNull
    @Override
    public ChapterVideoHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.recycle_view_item, viewGroup, false);
        return new ChapterVideoHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ChapterVideoHolder chapterVideoHolder, int i) {
        //修改子项的显示背景
        chapterVideoHolder.textView.setText(mDatas.get(i));
//        if (i == clickPos) {
//            chapterVideoHolder.textView.setBackgroundResource(R.drawable.xsl_chapter_item_selected);
//        } else {
//            chapterVideoHolder.textView.setBackgroundResource(R.color.translucent_background);
//        }

        chapterVideoHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListenner != null) {
                    int pos = chapterVideoHolder.getLayoutPosition();
                    itemClickListenner.onItemClick(chapterVideoHolder.itemView, pos);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        mDatas = new ArrayList<String>();
        mDatas = StringUtil.getChapterList();
        return mDatas.size();
    }

    public static class ChapterVideoHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public ChapterVideoHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.chapter_item);
        }
    }

    //点击某一条时候的回调
    public interface OnItemClickListenner {
        void onItemClick(View view, int position);
    }

    private OnItemClickListenner itemClickListenner;

    //设置监听器
    public void setOnItemClickListenner(ChapterAdapter.OnItemClickListenner listenner) {
        itemClickListenner = listenner;
    }

    public void setThisPostion(int pos) {
        clickPos = pos;
    }

    public String getPositionText(int pos) {
        return mDatas.get(pos);
    }
}
